package top.cflwork.controller;

import java.util.List;
import java.util.Map;

import com.xiaoleilu.hutool.date.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import top.cflwork.vo.ShopColCarVo;
import top.cflwork.service.ShopColCarService;
import top.cflwork.controller.BaseController;
import top.cflwork.util.PageUtils;
import top.cflwork.util.Query;
import top.cflwork.util.R;

/**
 * 收藏，或者购物车管理
 * 
 * @author cfl
 * @email 275300091@qq.com
 * @date 2020-04-21 13:56:56
 */
 
@Controller
@RequestMapping("/shopColCar")
public class ShopColCarController extends BaseController{
	@Autowired
	private ShopColCarService shopColCarService;
	
	@GetMapping("shopColPage")
	@RequiresPermissions("shopColCar:shopColPage")
	public String ShopColCar(){
	    return "/shopColCar/shopColCar";
	}
	@GetMapping("shopCarPage")
	@RequiresPermissions("shopColCar:shopCarPage")
	public String shopCarPage(){
		return "/shopColCar/shopCar";
	}
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("shopColCar:list")
	public PageUtils list(ShopColCarVo shopColCarVo){
		//查询列表数据
		shopColCarVo.setAdminId(getUserId());
		shopColCarVo.setType(0);
		List<ShopColCarVo> shopColCarList = shopColCarService.list(shopColCarVo);
		Long total = shopColCarService.count(shopColCarVo);
		PageUtils pageUtils = new PageUtils(shopColCarList, total);
		return pageUtils;
	}
	@ResponseBody
	@PostMapping("/listCar")
	@RequiresPermissions("shopColCar:listCar")
	public PageUtils listCar(ShopColCarVo shopColCarVo){
		//查询列表数据
		shopColCarVo.setAdminId(getUserId());
		shopColCarVo.setType(1);
		List<ShopColCarVo> shopColCarList = shopColCarService.list(shopColCarVo);
		Long total = shopColCarService.count(shopColCarVo);
		PageUtils pageUtils = new PageUtils(shopColCarList, total);
		return pageUtils;
	}
    @ResponseBody
    @PostMapping("/listAll")
    @RequiresPermissions("shopColCar:list")
    public List<ShopColCarVo> listAll(ShopColCarVo shopColCarVo){
        //查询列表数据
		shopColCarVo.setAdminId(getUserId());
        List<ShopColCarVo> shopColCarList = shopColCarService.list(shopColCarVo);
        return shopColCarList;
    }

	@GetMapping("/add")
	@RequiresPermissions("shopColCar:add")
	public String add(){
	    return "/shopColCar/add";
	}

	/*@GetMapping("/edit/{id}")
	@RequiresPermissions("shopColCar:edit")
	public String edit(@PathVariable("id") String id,Model model){
		ShopColCarVo shopColCar = shopColCarService.get(id);
		model.addAttribute("shopColCar", shopColCar);
	    return "/shopColCar/edit";
	}*/

    @GetMapping("/edit/{id}")
    @RequiresPermissions("shopColCar:edit")
    @ResponseBody
    public ShopColCarVo edit(@PathVariable("id") String id){
			ShopColCarVo shopColCar = shopColCarService.get(id);
        return shopColCar;
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shopColCar:add")
	public R save( ShopColCarVo shopColCar){
		shopColCar.setAdminId(getUserId());
		shopColCar.setAdminName(getUsername());
		shopColCar.setCreateTime(DateUtil.now());
		if(shopColCarService.save(shopColCar)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shopColCar:update")
	public R update( ShopColCarVo shopColCar){
		shopColCarService.update(shopColCar);
		return R.ok("修改成功");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shopColCar:remove")
	public R remove( String id){
		if(shopColCarService.remove(id)>0){
		return R.ok("删除成功");
		}
		return R.error("删除失败");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shopColCar:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		shopColCarService.batchRemove(ids);
		return R.ok("批量删除成功");
	}
    /**
     * 删除
     */
    @PostMapping( "/batchSave")
    @ResponseBody
    @RequiresPermissions("shopColCar:batchSave")
    public R batchSave(List<ShopColCarVo> shopColCarList){
			shopColCarService.batchSave(shopColCarList);
        return R.ok("批量新增成功");
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/saveColCar")
	public R saveColCar(@RequestBody ShopColCarVo shopColCar){
		shopColCar.setAdminId(getUserId());
		shopColCar.setAdminName(getUsername());
		shopColCar.setCreateTime(DateUtil.now());
		if(shopColCarService.save(shopColCar)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
}
